package com.ysd.lis.service.samsto.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.lis.mapper.samsto.SamstoSampleStorageMapper;
import com.ysd.lis.entity.samsto.SamstoSampleStorage;
import com.ysd.lis.service.samsto.SamstoSampleStorageService;
import com.ysd.lis.request.samsto.SamstoSampleStorageParam;
import org.springframework.stereotype.Service;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ysd.common.Result;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.lis.entity.SysUser;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 样本库表(SamstoSampleStorage)表服务实现类
 *
 * @author leiming
 * @since 2024-10-26 09:27:42
 */
@Service("samstoSampleStorageService")
public class SamstoSampleStorageServiceImpl extends ServiceImpl<SamstoSampleStorageMapper, SamstoSampleStorage> implements SamstoSampleStorageService {

    private MPJLambdaWrapper<SamstoSampleStorage> initWrapper(){
        MPJLambdaWrapper<SamstoSampleStorage> lambdaQueryWrapper = new MPJLambdaWrapper<>();
		//ID 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getId, SamstoSampleStorage::getId);
		//标本ID 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getSpecimenId, SamstoSampleStorage::getSpecimenId);
		//标本类型 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getSpecimenType, SamstoSampleStorage::getSpecimenType);
		//工作组 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getWorkGroup, SamstoSampleStorage::getWorkGroup);
		//操作人 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getOperator, SamstoSampleStorage::getOperator);
		//所在冰箱 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getFridgeId, SamstoSampleStorage::getFridgeId);
		//所在层 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getLayerId, SamstoSampleStorage::getLayerId);
		//标本盒表 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getSpecboxId, SamstoSampleStorage::getSpecboxId);
		//库位号 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getOrderNumber, SamstoSampleStorage::getOrderNumber);
		//仓库位置 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getLocation, SamstoSampleStorage::getLocation);
		//检验时间 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getInspectionTime, SamstoSampleStorage::getInspectionTime);
		//入库时间 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getInTime, SamstoSampleStorage::getInTime);
		//存储到期时间 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getExpirationTime, SamstoSampleStorage::getExpirationTime);
		//机构 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getOrgId, SamstoSampleStorage::getOrgId);
		//状态 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getStatus, SamstoSampleStorage::getStatus);
		//所属科室 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getDeptName, SamstoSampleStorage::getDeptName);
		//删除标记 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getDelFlag, SamstoSampleStorage::getDelFlag);
		//创建人 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getCreator, SamstoSampleStorage::getCreator);
		//创建时间 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getCreateTime, SamstoSampleStorage::getCreateTime);
		//修改人 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getEditor, SamstoSampleStorage::getEditor);
		//修改时间 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getEditTime, SamstoSampleStorage::getEditTime);
		//删除时间 
		lambdaQueryWrapper.selectAs(SamstoSampleStorage::getDelTime, SamstoSampleStorage::getDelTime);
        return lambdaQueryWrapper;
    }

    @Override
    public Result findPageList(SamstoSampleStorageParam pageParam) {
        Page<SamstoSampleStorage> pageList=null;
        LambdaQueryWrapper<SamstoSampleStorage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SamstoSampleStorage> page = new Page<SamstoSampleStorage>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        lambdaQueryWrapper.eq(SamstoSampleStorage::getDelFlag, 0);
        /*if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SamstoSampleStorage::getBoxDoor, pageParam.getKeywords())
                    .or().like(SamstoSampleStorage::getModelName, pageParam.getKeywords())
                    .or().like(SamstoSampleStorage::getBrandName, pageParam.getKeywords()));
        }*/
        lambdaQueryWrapper.orderByAsc(SamstoSampleStorage::getCreateTime);
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                lambdaQueryWrapper.eq(SamstoSampleStorage::getOrgId, sysUser.getOrgId());
            }
            pageList= this.baseMapper.selectPage(page,lambdaQueryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(1, "获取信息成功", pageList);
    }

    @Override
    public Result findListByGId(SamstoSampleStorageParam pageParam) {
        Page<SamstoSampleStorage> pageList=null;
        LambdaQueryWrapper<SamstoSampleStorage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<SamstoSampleStorage> page = new Page<SamstoSampleStorage>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        /*if(StringUtils.isNotBlank(pageParam.getDeptName())){
            lambdaQueryWrapper.eq(SamstoSampleStorage::getDeptName, pageParam.getDeptName());
        }
        lambdaQueryWrapper.eq(SamstoSampleStorage::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getKeywords())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SamstoSampleStorage::getBoxDoor, pageParam.getKeywords())
                    .or().like(SamstoSampleStorage::getModelName, pageParam.getKeywords())
                    .or().like(SamstoSampleStorage::getBrandName, pageParam.getKeywords()));
        }*/
        lambdaQueryWrapper.orderByAsc(SamstoSampleStorage::getCreateTime);
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                lambdaQueryWrapper.eq(SamstoSampleStorage::getOrgId, sysUser.getOrgId());
            }
            pageList= this.baseMapper.selectPage(page,lambdaQueryWrapper);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return Result.succ(1, "获取信息成功", pageList);
    }

    @Override
    public Result getInfoById(String id) {
        SamstoSampleStorage SamstoSampleStorage=this.baseMapper.selectById(id);
        return Result.succ(1, "获取信息成功", SamstoSampleStorage);
    }

    @Override
    public Result add(SamstoSampleStorageParam param) {
        SamstoSampleStorage info=new SamstoSampleStorage();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setCreator(sysUser.getId());
                param.setOrgId(sysUser.getOrgId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setCreateTime(new Date());
        BeanUtil.copyProperties(param,info);
        info.setDelFlag(0);
        info.setStatus(1);
        this.baseMapper.insert(info);
        return Result.succ(1, "添加信息成功", null);
    }

    @Override
    public Result edit(SamstoSampleStorageParam param) {
        Map returnMap = new HashMap();
        SamstoSampleStorage info=new SamstoSampleStorage();
        try{
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                param.setEditor(sysUser.getId());
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        param.setEditTime(new Date());
        BeanUtil.copyProperties(param,info);
        this.baseMapper.updateById(info);
        returnMap.put("id", param.getId());
        return Result.succ(1, "修改信息成功", returnMap);
    }

    @Override
    public Result delete(List<String> ids) {
        Result result;
        try {
            boolean b = this.removeByIds(ids);
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result updelete(List<String> ids) {
        Result result;
        LambdaUpdateWrapper<SamstoSampleStorage> lambdaUpdateWrapper = new UpdateWrapper<SamstoSampleStorage>().lambda();
        // set 表示要更新的字段， eq 表示  WHERE 条件
        lambdaUpdateWrapper.set(SamstoSampleStorage::getDelFlag,1)
                .in(SamstoSampleStorage::getId, ids);
        this.baseMapper.update(new SamstoSampleStorage(), lambdaUpdateWrapper);

        try {
            int b= this.baseMapper.update(new SamstoSampleStorage(), lambdaUpdateWrapper);
            if (b>0) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }
    
    @Transactional
    @Override
    public Result saveOrUpdateList(SamstoSampleStorageParam param) {
        Result result;
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isEmpty(sysUser)) {
               return Result.fail(304, "请重新登陆", "");
            }
            for (SamstoSampleStorage info : param.getInfoList()) {
                if(StringUtils.isNotBlank(info.getId())){
                    info.setEditor(sysUser.getId());
                    info.setEditTime(new Date());
                    this.baseMapper.updateById(info);
                }else{
                    info.setCreator(sysUser.getId());
                    info.setOrgId(sysUser.getOrgId());
                    info.setCreateTime(new Date());
                    info.setDelFlag(0);
                    info.setStatus(1);
                    this.baseMapper.insert(info);
                }
            }
            Boolean b= this.saveOrUpdateBatch(param.getInfoList());
            if (b) {
                result = Result.succ(1, "保存成功", null);
            } else {
                result = Result.fail(400, "保存失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "保存失败", "");
        }
        return result;
    }
}

